package A099queue;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

public class A002_ArrayBlockingQueue_add_remove_抛出异常 {

    public static void main(String[] args) {
        BlockingQueue<String> blockingQueue = new ArrayBlockingQueue<>(3);

        System.out.println("blockingQueue.add(\"a\") \t" + blockingQueue.add("a"));
        System.out.println("blockingQueue.add(\"b\") \t" + blockingQueue.add("b"));
        System.out.println("blockingQueue.add(\"c\") \t" + blockingQueue.add("c"));

        try {
            //抛出 java.lang.IllegalStateException: Queue full
            System.out.println("blockingQueue.add(\"d\") \t" + blockingQueue.add("d"));
        } catch (Exception e) {
            System.out.println("blockingQueue.add(\"d\") \t" + e.getMessage());
        }

        System.out.println("blockingQueue.element() \t" + blockingQueue.element());

        System.out.println("blockingQueue.remove()\t" + blockingQueue.remove());
        System.out.println("blockingQueue.remove()\t" + blockingQueue.remove());
        System.out.println("blockingQueue.remove()\t" + blockingQueue.remove());


        try {
            //抛出 java.util.NoSuchElementException
            System.out.println("blockingQueue.remove()\t" + blockingQueue.remove());
        } catch (Exception e) {
            System.out.println("blockingQueue.remove()\t" + e.getMessage());
        }

        try {
            //element()相当于peek(),但element()会抛NoSuchElementException
            System.out.println("blockingQueue.element() \t" + blockingQueue.element());
        } catch (Exception e) {
            System.out.println("blockingQueue.element() \t" + e.getCause());
        }

    }
}
